
import { diyLinkType, Result, ResultError } from "@/types";
import { inArray } from "./data/array";
import { setStorage } from "@/utils/tools"

// 获取首页
export const getRoutePageHome = (key: string | number | undefined) => {
    if (key === undefined) return '';
    let tab: any = {
        store_goods_categroy: '/pages/goods_categroy/index',
        store_user: '/pages/user/index',
        store_cart: '/pages/cart/index',
        store_home: '/pages/index/index',
    };
    return tab[key] ? tab[key] : '';
}

export const tabbarList = [
    '/pages/index/index',
    '/pages/goods_categroy/index',
    '/pages/cart/index',
    '/pages/user/index'
];
/**
 * 页面跳转 
 */
export const routeRedirectTo = (to: any, param: any = {}, mode: string = '') => {
    if (to === undefined) {
        return false;
    }
    let url = to;

    if (typeof to == 'string' && getRoutePageHome(to)) {
        uni.switchTab({
            url: getRoutePageHome(to),
        })
        return;
    } else if (typeof to == 'object' || (typeof to == 'string' && inArray(to, tabbarList))) {
        if ((to?.key != undefined && getRoutePageHome(to.key))) {
            uni.switchTab({
                url: getRoutePageHome(to.key),
            })
            return;
        } else if (inArray(to, tabbarList)) {
            uni.switchTab({
                url: to,
            })
            return;
        } else {
            var page = getRoutePageCheck(to);
            if (to.key && getRoutePageHome(to.key)) {
                mode = 'tabbar';
            }
            if (page != '') url = page;
        }
    }
    if (param != undefined) {
        Object.keys(param).forEach(function (key) {
            if (typeof url == 'string' && url.indexOf('?') != -1) {
                url += "&" + key + "=" + param[key];
            } else {
                url += "?" + key + "=" + param[key];
            }
        });
    }
    switch (mode) {
        case 'tabbar':
            // 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
            uni.switchTab({
                url: url
            })
            break;
        case 'redirectTo':
            // 关闭当前页面，跳转到应用内的某个页面。
            uni.redirectTo({
                url: url
            });
            break;
        case 'reLaunch':
            // 关闭所有页面，打开到应用内的某个页面。
            uni.reLaunch({
                url: url
            });
            break;
        default:
            // 保留当前页面，跳转到应用内的某个页面
            uni.navigateTo({
                url: url
            });
    }
}

// 检测返回url
export const getRoutePageCheck = (link: diyLinkType) => {
    let url = getPageUrl(link?.key);
    var PageHome = getRoutePageHome(link?.key);
    if (PageHome) {
        url = PageHome;
        setRoutePageValue(link.value, link.key);
    } else if (link?.value) {
        url += String(link.value);
    }
    if (!url) {
        var d: any = {
            store_user_address: '/otherpages/user/address/index',
            store_user_collection: '/otherpages/user/collection/index',
            store_user_coupon: '/otherpages/user/coupon/index',
            store_user_order: '/otherpages/user/order/list',
            store_user_after: '/otherpages/user/after/after',
            store_community: '/otherpages/community/index',
            store_help: '/otherpages/help/index',
            store_notice: '/otherpages/notice/index',
            store_goods_list: '/pages/goods/list/list',
            store_search: '/pages/search/index',
            store_customer: '/pages/customer/index',
            store_user_footprint: '/pages/user_footprint/index',
            store_user_water: '/pages/user_water/index',
            store_user_wallet: '/pages/user_wallet/index',
            store_shop: '/pages/shop/shop',
            store_user_phone: '/pages/user_phone/index',
            store_live: '/pages/live_list/index',
            store_diy_page: '/pages/diy/index',
        };
        url = link.key ? d[link.key] || '' : '';
    }
    return url;
}


// 获取url
export const getPageUrl = (k: string | number | undefined) => {
    if (k == undefined) return false;
    var u = '',
        data: any = {
            store_user_order: {
                url: "/otherpages/user/order/list",
                key: "status",
            },
            store_diy_page: {
                url: "/pages/diy/diy",
                key: "page_id",
            },
            store_goods: {
                url: "/pages/goods/detail/detail",
                key: "goods_id",
            },
        };
    if (!data[k]) return false;
    u = data[k].url;
    if (data[k].key !== '') u += '?' + data[k].key + '='
    return u;
}

export const setRoutePageValue = (v: any, k: string | number | undefined = 'store_goods_categroy') => {
    switch (k) {
        case 'store_goods_categroy':
            // store.commit('setGoodsCategoryId', v);
            break;
    }
}


export const getTabIndex = (pages: any) => {
    let index = 0, DIYNAVDESIGN = wx.getStorageSync("DIYNAVDESIGN");
    var currPage = pages[pages.length - 1];
    if (typeof DIYNAVDESIGN != 'object') {
        return index;
    }
    var nav = DIYNAVDESIGN.data
    for (var i = 0; i < nav.length; i++) {
        var link = nav[i].link_url;
        if (getRoutePageHome(link.key) == '/' + currPage.route) {
            index = i;
        }
    }
    return index;
}


export default {
    tabbarList,
    getRoutePageHome,
    routeRedirectTo
}